#include<stdio.h>
#include<mpi.h>

int main(int argc, char** argv)
{
    int rank, size;
    float pi = 0.0, BUFFER_PI = 0.0, x;

    MPI_Init(&argc, &argv);
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);
    MPI_Comm_size(MPI_COMM_WORLD, &size);

	float w = 1.0 / size;

    x = w * ((rank + 1) - 0.5);
    pi = pi + 4.0 / (1.0 + (x * x));    

    MPI_Reduce(&pi, &BUFFER_PI, 1, MPI_FLOAT, MPI_SUM, 0, MPI_COMM_WORLD);

    if(rank == 0) {
        BUFFER_PI = BUFFER_PI * w;
        printf("PI e aproximadamente: %10.8f\n", BUFFER_PI);
    }

    MPI_Finalize();
    return 0;
}
